<?php

namespace App\Models\Admin;

use App\Log\GoodsCategoryAdminLog;
use Illuminate\Database\Eloquent\Model;
use DB,Saas;

class GoodsCategory extends Base
{
    const OPERATE_PID = 2;
    const MODEL = 'goods_category';

    protected $table = 'goods_category';

    public $timestamps = false;

    protected $guarded = [];

    public function selfNext()
    {
        return $this->hasMany($this,'pid');
    }

    public static function getNextIds($id)
    {
        $ids = self::where('pid',$id)->pluck('id');
        if($ids->isEmpty()){
            return [$id];
        }else{
            return $ids->toArray();
        }
    }

    public static function getCategorys()
    {
        $list=self::where('company_id',0)->orderBy('pid','asc')->orderBy('ordid','asc')->get();
        if(empty($list))
        {
            return $list;
        }
        $list=self::generateTree($list);
        return $list;
    }

    public static function addLog($pre,$next,$type,$method='')
    {
        $info = AdminOperateModule::where('pid',self::OPERATE_PID)->where('type',$type)->where('model',self::MODEL)->when($method,function ($query)use ($method){
            $query->where('method',$method);
        })->first();
        if (!empty($info)){
            $goodsCategoryLog = new GoodsCategoryAdminLog($pre,$next,$info);
            $goodsCategoryLog->addLog();
        }
    }

}
